<template>
    <div class="header">
        <div class="logo" style="margin-left: 20px;color: #000">
            <el-button v-if="currentRoute !== 'home'" type="text" icon="el-icon-arrow-left"
                       style="font-size: 22px; color: #000" @click="backHome()">首页 |
            </el-button>
            VDC配置中心
        </div>
        <div class="header-right">
            <div class="header-user-con">

            
                <!-- 消息中心 -->
                <!-- <div class="btn-bell">
                        <el-tooltip effect="dark" :content="message?`有${message}条未读消息`:`消息中心`" placement="bottom">
                            <router-link to="/tabs">
                                <i class="el-icon-bell"></i>
                            </router-link>
                        </el-tooltip>
                        <span class="btn-bell-badge" v-if="message"></span>
                    </div> -->
                <!-- 用户头像 -->
<!--                <div class="user-avator">-->
<!--                    <img src="../assets/img/img.jpg"/>-->
<!--                </div>-->
                <!-- 用户名下拉菜单 -->
                <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                <span class="el-dropdown-link">
                    {{ user_name }}
                    <i class="el-icon-caret-bottom"></i>
                </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item disabled command="user">个人中心</el-dropdown-item>
                            <!-- <el-dropdown-item command="dashboard">系统配置</el-dropdown-item> -->

                            <!-- <el-dropdown-item command="version">历史版本</el-dropdown-item> -->

                            <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </div>
        </div>
    </div>
</template>

<script>
import {
    computed,
    onMounted,
    ref
} from "vue";
import {
    useStore
} from "vuex";
import {
    useRouter
} from "vue-router";
import {
    logout
} from '../api/user.js';
import {
    setCookie
} from "../utils/util.js";

export default {
    setup() {
        let user_name = ref('');
        try {
            user_name.value = JSON.parse(localStorage.getItem("userInfo")).username;

        } catch (error) {

        }
        const message = 2;

        const store = useStore();

        const collapse = computed(() => store.state.collapse);
        // 侧边栏折叠
        const collapseChage = () => {
            store.commit("handleCollapse", !collapse.value);
            // store.commit("handleCollapse", true);
        };

        onMounted(() => {
            store.commit("handleCollapse", false);
            if (document.body.clientWidth < 1600) {
                store.commit("handleCollapse", true);
            }
        });


        window.addEventListener('resize', () => {
            store.commit("handleCollapse", false);
            if (document.body.clientWidth < 1600) {
                store.commit("handleCollapse", true);
            }
        });
        // 用户名下拉菜单选择事件
        const router = useRouter();
        const currentRoute = router.currentRoute.value.name;
        const handleCommand = (command) => {
            const ENV = import.meta.env;

            if (command == "loginout") {
                logout().then((res) => {
                    if (res.data.code == 200) {
                        localStorage.removeItem("ms_userMail");
                        localStorage.removeItem("ms_userName");
                        localStorage.removeItem("ms_userId");
                        localStorage.removeItem("ms_userNo");
                        localStorage.removeItem("userInfo");
                        setCookie("userInfo", '', -100000);


                        const ENV = import.meta.env;

                        if (ENV.VITE_NAME_MODE === "dev") {
                            window.location.href = 'https://autodrive-test.hozonauto.com/#/login';
                        } else if (ENV.VITE_NAME_MODE === "test") {
                            window.location.href = 'https://autodrive-test.hozonauto.com/#/login';
                        } else if (ENV.VITE_NAME_MODE === "pro") {
                            window.location.href = 'https://autodrive.hozonauto.com/#/login';
                        } else if (ENV.VITE_NAME_MODE === "beta") {
                            window.location.href = 'https://autodrive-beta.hozonauto.com/#/login';
                        }
                    }
                }, function (response) {
                })


            } else if (command == "version") {
                // window.location.href ='localhost:8080/#/version'
                window.open('localhost:8080/#/version')
            } else if (command == "user") {
                router.push("/user");
            } else if (command == "dashboard") {
                router.push("/dashboard");
            }
        };

        const toMyTask = () => {
            router.push("/task");
        };

        const toMyAdvice = () => {
            router.push("/advice");
        };

        const toMyCarInfo = () => {
            router.push("/carInfo");
        };

        const ENV = import.meta.env;

        const backHome = () => {

            if (ENV.VITE_NAME_MODE === 'dev') {
                window.location.href =
                    'https://autodrive-test.hozonauto.com/#/platformHome';
            } else if (ENV.VITE_NAME_MODE === 'test') {
                window.location.href =
                    'https://autodrive-test.hozonauto.com/#/platformHome';
            } else if (ENV.VITE_NAME_MODE === 'pro') {
                window.location.href =
                    'https://autodrive.hozonauto.com/#/platformHome';
            } else if (ENV.VITE_NAME_MODE === 'beta') {
                window.location.href =
                    'https://autodrive-beta.hozonauto.com/#/platformHome';
            }

            sessionStorage.removeItem('tagsLists')
        };
        return {
            user_name,
            message,
            collapse,
            collapseChage,
            handleCommand,
            toMyTask,
            toMyAdvice,
            toMyCarInfo,
            backHome,
            currentRoute,
        };
    },
};
</script>

<style scoped>
.header {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    font-size: 22px;
    color: #000;

    background: #fff;
    border-bottom: 1px solid #ccc;
}

.collapse-btn {
    float: left;
    padding: 0 21px;
    cursor: pointer;
    line-height: 70px;
}

.header .logo {
    float: left;
    line-height: 70px;
}

.header-right {
    float: right;
    padding-right: 50px;
}

.header-user-con {
    display: flex;
    height: 70px;
    align-items: center;

}

.btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
}

.btn-bell,
.btn-fullscreen {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
}

.btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #000;
}

.btn-bell .el-icon-bell {
    color: #fff;
}

.user-name {
    margin-left: 10px;
}

.user-avator {
    margin: 10px;
}

.user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.el-dropdown-link {
    color: #000;
    cursor: pointer;
}

.el-dropdown-menu__item {
    text-align: center;
}
</style>
